/*
 * 文件名：IntegrationServiceImpl.java	 
 * 时     间：下午9:35:39
 * 作     者：lyf       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.tools.util.Tools;
import com.local.dao.mapers.IntegrationMapper;
import com.local.model.Integration;
import com.local.model.IntegrationExample;
import com.local.page.IntegrationPage;
import com.local.service.face.IntegrationService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: IntegrationServiceImpl
 * @author Administrator
 * @date 2015年10月12日 下午9:35:39
 */
@Service
public class IntegrationServiceImpl extends
		BaseServiceImpl<IntegrationExample, Integration, String> implements
		IntegrationService
{
	
	@Autowired
	private IntegrationMapper interMapper;
	
	@Override
	public Integer addOrUpdateByCarNo(Integration interIntegration)
			throws BaseException
	{
		try
		{
			
			if (Tools.isEmpty(interIntegration.getCarNo()))
			{
				throw new BaseException("50000-00001-00039");
			}
			Integration interInfo = this.queryInterationByCarNo(interIntegration.getCarNo());
			if(!Tools.isEmpty(interIntegration.getUuid())){
				interInfo=interMapper.selectByPrimaryKey(interIntegration.getUuid());
			}
			interIntegration.setStatus(1);
			
			if (!Tools.isEmpty(interInfo))
			{
			
				interIntegration.setUuid(interInfo.getUuid());
				interMapper.updateByPrimaryKeySelective(interIntegration);
				return 1;
			}
			if(Tools.isEmpty(interIntegration.getAttr1())){
				interIntegration.setAttr1("1");
			}
			interIntegration.setUuid(UUIDGenerateUtil.generate());
			interMapper.insertSelective(interIntegration);
			
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage(), t);
		}
		return null;
	}
	
	@Override
	public Integration queryInterationByCarNo(String carNo)
			throws BaseException
	{
		IntegrationExample condition = new IntegrationExample();
		condition.or().andCarNoEqualTo(carNo);
		List<Integration> list = interMapper.selectByCondition(condition);
		return list.isEmpty() ? null : list.get(0);
	}
	
	@Override
	public BaseMapper<IntegrationExample, Integration, String> getMapper()
	{
		
		return interMapper;
	}

	@Override
	public List<Integration> queryMemberFreeQueryForm(
			IntegrationPage<Integration> pageInfo)
	{
		// TODO Auto-generated method stub
		Integer recordCount = interMapper.countAllByPage(pageInfo);
		pageInfo.setTotalRecords(recordCount);
		List<Integration> list = interMapper.queryByPage(pageInfo);
		pageInfo.setData(list);
		return list;
	}
	
}
